<?php
class NewsController extends AppController {
	var $name = 'News';
	var $components = array('Uploader.Uploader');
	var $errors = array();
	//var $uses=array('Image');

	function beforeFilter() {
		parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/news/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '5M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;
		$this->Auth->allow('chitiet','pages','listnews','home');
	}
	function home() {
		// $this->loadModel("Slide");
		// $hSlide = $this->Slide->find('all');
		// $this->loadModel("Cateservice");
		//$this->layout=false;
		$this->loadModel("Cateproduct");
		$this->loadModel("News");
		$this->loadModel("Banner");
		$this->loadModel("Gallery");

		$this->Cateservice->recursive = -1;
		$this->Cateproduct->recursive = -1;

		//$hService = $this->Cateservice->find('all',array('conditions'=>array('status'=>2,'lang'=>$this->lg),"limit"=>6));
		$hProduct = $this->Cateproduct->find('all',array('conditions'=>array('status'=>2,'lang'=>$this->lg),"limit"=>60,'order'=>'order ASC'));
		$hNews = $this->News->find('all',array('conditions'=>array('status'=>2,'catenews_id'=>1,'lang'=>$this->lg),'limit'=>3));
		$hPartner = $this->Guest->find('all', array('conditions'=>array('Guest.status'=>2),'limit'=>60,'order'=>array('Guest.id'=>'DESC')));
		$hBanners = $this->Banner->find('all',array('conditions'=>array('Banner.status'=>2)));
		$hgalleries = $this->Gallery->find('all',array('conditions'=>array('Gallery.status'=>2)));

		$this->set(compact(/*'hSlide','hService',*/'hProduct','hPartner','hNews','hBanners','hgalleries'));
	}
	function index($fid=NULL) {
		$this->News->recursive = 1;
		$conds = array('News.status' =>2,'catenews_id'=>1,'News.lang'=>$this->lg);

		$this->paginate = array(
			'conditions'=>$conds,
			'limit'=>10,
			'order'=>'News.id DESC'
		);
		$this->set('news', $this->paginate());
	}
	function chitiet($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$conditions = array('News.status'=>2);
		$news = $this->News->read(null, $id);
		$this->set('news',$news);
		$othernews = $this->News->find('all',array('conditions'=>array('News.id <>'=>$id,'News.status'=>2,'News.lang'=>$this->lg,'catenews_id'=>$news['News']['catenews_id']),'limit'=>10,'order'=>'News.id DESC'));
		$this->set(compact('othernews'));
	}
	function admin_index() {

		$this->layout='admin';
		$this->News->recursive = 0;
		$conditions = '';
		$key = $this->data['News']['key'];
		if($key!= null){
			$conditions = array('News.title LIKE '=>'%'.$key.'%');
		}
		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'News.id DESC'
		);
		$this->set('news', $this->paginate());
		//debug($this->paginate);
		$this->set(compact('key'));
	}


	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('news', $this->News->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$filename = $this->Uploader->upload('image');
			$this->News->create();
			$users_id = $this->Auth->user('id');
			$this->data['News']['image']  = $filename['name'];
			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
	}

	function admin_edit($id = null) {
		$files = $this->News->find('list',array(
			'conditions'=>array('News.id'=>$id),
			'fields'=>'image')
		);
		//debug($files);
		$file ='';
		$file = "files/uploads/news/" .$files[$id];
		//debug($file);
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['News']['image'] = $files[$id];
            if ($data = $this->Uploader->upload('image')) {
				$tmpname  = $data['name'];
				if(is_file($file)){
					unlink($file);
				}
				$this->data['News']['image'] = $tmpname;
            }
			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->News->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này !', true));
			$this->redirect(array('action'=>'index'));
		}
		if($id){
			$files = $this->News->find('first',array(
			'conditions'=>array('News.id'=>$id),
			'fields'=>'image')
		);
		$file = "files/uploads/news/".$files['News']['image'];
			if(is_file($file)){
				unlink($file);
			}
		}
		if ($this->News->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được bài viết !', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được bài viết !', true));
    		$this->redirect(array('action' => 'index'));
        }
         foreach($arrid as $item){
			if($item != null){
				$files = $this->News->find('first',array(
				'conditions'=>array('News.id'=>$item),
				'fields'=>'image')
				);
				$file = "files/uploads/news/".$files['News']['image'];
				if(is_file($file)){
					unlink($file);
				}
			}
    		$this->News->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index'));
    }
	function admin_delfile($id = null){
		$files = $this->News->find('first',array(
			'conditions'=>array('News.id'=>$id),
			'fields'=>'image')
			);
		$file = "files/uploads/news/".$files['News']['image'];
		if(is_file($file)){
			unlink($file);
		}
		$this->News->query("Update tblnews set image = '' where id='".$id."'");
    }
    function admin_count(){
		$count = 0;
		$file = "./files/cou6nter.txt";
		if(file_exists($file)) {
			$fil = fopen($file, "r");
			$count = fread($fil, 8);
			fclose($fil);
		}
		if($this->data){
			$fil = fopen($file, "w");
			fwrite($fil, $this->data["News"]['count']);
			fclose($fil);
			$count = $this->data["News"]['count'];
		}
		$this->set(compact('count'));

	}
}
?>